//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics;
using System.IO;

namespace OpenLS.Spreadsheet.UI
{
    public class DebugTraceListener : TraceListener
    {
        public static DebugTraceListener Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new DebugTraceListener();
                return _instance;
            }
        }
        static DebugTraceListener _instance;
        DebugTraceListener()
        {
            FileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().Replace("-", "")+".Kherty.debug.txt");
//\\            Name = System.IO.Path.GetTempPath() System.IO.Path.GetTempFileName();
            //\\writer = new System.IO.StreamWriter(Name);
            creationDate = DateTime.Now;
        }

        private readonly DateTime creationDate;
        public readonly string FileName;
       //\\ System.IO.TextWriter writer;
        public override void Write(string message)
        {
            try
            {
                TextWriter writer = new StreamWriter(FileName, true);
                writer.Write(message);
                writer.Flush();
                writer.Close();
            }
            catch (Exception)
            { }
        }

        public override void WriteLine(string message)
        {
            DateTime dt = DateTime.Now;
            string s1 = (string.Format("[{0}/{1}/{2} {3}:{4}:{5}.{6}]", dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond));
            TimeSpan ts = dt - creationDate;
            Write(string.Format("{0}\t{1}\t{2}{3}", s1, ts.TotalMilliseconds, message, Environment.NewLine));
        }
    }

}
